import 'dart:math';

import 'package:flutter/material.dart';
import 'package:x3_charts_core/model/equally_divided_pie_chart_data_model.dart';

///**
///* @author hrongyong
///* @date   2/4/21
///* @email hrongyong@gmail.com
///* Description: 等分饼图
///*/
class EquallyDividedPieChartPainter extends CustomPainter {
  final List<EquallyDividedPieChartDataModel> pieChartList;

  ///主副标题大小
  final double fontSize;

  EquallyDividedPieChartPainter({@required this.pieChartList, this.fontSize = 4}) {
    pieAngle = pieChartList.length > 0 ? (2 * pi) / pieChartList.length : 0.0;
    fillPaint = Paint()
      ..isAntiAlias = true
      ..style = PaintingStyle.fill;
    strokePaint = Paint()
      ..isAntiAlias = true
      ..color = Colors.white
      ..style = PaintingStyle.stroke;
  }

  ///最大半径
  double maxRadius = 0.0;

  ///由于怕数值过小看不到，所以将饼图等分
  ///每块饼的角度
  double pieAngle = 0.0;

  ///饼块填充
  Paint fillPaint;

  ///饼块描边
  Paint strokePaint;

  ///文字
  final TextPainter _textPainter = TextPainter(textDirection: TextDirection.ltr);
  final TextPainter _subTextPainter = TextPainter(textDirection: TextDirection.ltr);

  @override
  void paint(Canvas canvas, Size size) {
    maxRadius = size.shortestSide / 2;

    canvas.translate(size.width / 2, size.height / 2);

    //绘制饼图
    drawPie(canvas, size);

    //绘制圆心
    canvas.drawCircle(Offset.zero, maxRadius * 0.2, Paint()..color = Colors.grey[300]);
    //绘制文字
    drawText(canvas, size);
  }

  ///绘制文字
  void drawText(Canvas canvas, Size size) {
    canvas.save();
    canvas.rotate(2 * pi);
    for (int i = 0; i < pieChartList.length; i++) {
      //当前饼块的半径
      double radius = maxRadius * 0.8 * pieChartList[i].proportion + maxRadius * 0.2;

      ///保存画布
      canvas.save();
      canvas.rotate(pieAngle / 2 + pieAngle * i);
      _drawAxisText(
        canvas,
        size,
        offset: Offset(0, -radius),
        title: pieChartList[i].name,
        subTitle: pieChartList[i].subTitle,
        color: pieChartList[i].color,
      );
      canvas.restore();
    }
    canvas.restore();
  }

  void _drawAxisText(
    Canvas canvas,
    Size size, {
    String title,
    String subTitle,
    Offset offset = Offset.zero,
    int index,
    Color color,
  }) {
    ///绘制标题
    TextSpan text = TextSpan(text: title, style: TextStyle(fontSize: fontSize, color: color));
    _textPainter.text = text;
    _textPainter.layout(); // 进行布局
    Size textSize = _textPainter.size;

    ///绘制副标题
    TextSpan subText = TextSpan(text: subTitle, style: TextStyle(fontSize: fontSize, color: color));
    _subTextPainter.text = subText;
    _subTextPainter.layout(); // 进行布局
    Size subTextSize = _subTextPainter.size;

    Offset textOffsetPos = Offset(offset.dx - textSize.width / 2, offset.dy - textSize.height - subTextSize.height);
    Offset subTextOffsetPos = Offset(offset.dx - subTextSize.width / 2, offset.dy - subTextSize.height);

    _textPainter.paint(canvas, textOffsetPos);
    _subTextPainter.paint(canvas, subTextOffsetPos);
  }

  ///绘制饼图
  void drawPie(Canvas canvas, Size size) {
    canvas.save();
    // 由于希望一开始从 Y 轴负向开始绘制，可以使用 canvas.rotate(-pi / 2); 操作画布。
    canvas.rotate(-pi / 2);
    for (int i = 0; i < pieChartList.length; i++) {
      //当前饼块的半径
      double radius = maxRadius * 0.8 * pieChartList[i].proportion + maxRadius * 0.2;
      Path path = Path();
      path.lineTo(radius, 0);
      path.arcTo(Rect.fromCenter(center: Offset.zero, width: radius * 2, height: radius * 2), 0, pieAngle, false);
      path.close();
      canvas.drawPath(path, strokePaint);
      canvas.drawPath(path, fillPaint..color = pieChartList[i].color);
      canvas.rotate(pieAngle);
    }

    canvas.restore();
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
}
